package com.nuyoah.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuyoah.common.dto.LoginDto;
import com.nuyoah.common.dto.UserDto;
import com.nuyoah.entity.Permission;
import com.nuyoah.entity.Role;
import com.nuyoah.entity.User;
import com.nuyoah.mapper.UserMapper;
import com.nuyoah.service.PermissionService;
import com.nuyoah.service.RoleService;
import com.nuyoah.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 存储所有用户的基本信息 服务实现类
 * </p>
 *
 * @author xyx
 * @since 2024-06-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;


    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    @Override
    public User getByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username", username));
    }


    @Override
    public void insertUserRoleBinding(Integer id, int i) {
        userMapper.insertUserRoleBinding(id, i);
    }

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public User getUserById(Integer ids) {
        return userMapper.getUserById(ids);
    }

    @Override
    public User login(LoginDto loginDto) {
        User user = userMapper.getUserByUsername(loginDto.getUsername());
        if (user == null) {
            return null;
        }

        StpUtil.login(user.getId());
        user.setToken(StpUtil.getTokenValue());

        return user;
    }

    @Override
    public String addUser(User user) {
        //判断用户是否存在
        User user1 = userMapper.getUserByUsername(user.getUsername());
        if (user1 != null) {
            return "用户名已存在";
        }
        //密码加密
        user.setPassword(DigestUtil.md5Hex(user.getPassword()));
        //添加用户
        userMapper.insert(user);
        return "添加成功";
    }

    @Override
    public String updateUser(User user) {
        //判断用户是否存在
        User user1 = userMapper.getUserByUsername(user.getUsername());
        if (user1 == null) {
            return "用户不存在";
        }
        //密码加密
        user.setPassword(DigestUtil.md5Hex(user.getPassword()));
        //更新用户
        userMapper.updateById(user);
        return "更新成功";
    }

    @Override
    public String deleteUser(Integer id) {
        //判断用户是否存在
        User user = userMapper.selectById(id);
        if (user == null) {
            return "用户不存在";
        }

        //删除用户
        userMapper.deleteById(id);
        return "删除成功";
    }

    @Override
    public String existUser(Integer id) {
        //判断用户是否存在
        User user = userMapper.selectById(id);
        if (user == null) {
            return "用户不存在";
        }
        return "用户存在";
    }
}

